﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNet.Auth.Service;
using DotNet.Edu.Service;
using DotNet.Edu.WebUtility;
using DotNet.Extensions;
using DotNet.Helper;

namespace DotNet.StudentWap.Controllers
{
    public class DefaultController : StudentWapController
    {
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(string account, string password)
        {
            var studentService = EduService.Student;
            var result = studentService.Login(account, password);
            if (result.Success) //登陆成功
            {
                FormsAuthentication.SetAuthCookie(account, false);
                var entity = studentService.GetByIDCardNoOrMobile(account);
                studentService.UpdateExamStatus(entity);

                var audit = EduWebHelper.BuildStudentAuditEntity(entity);
                EduService.StudentAudits.Create(audit);

                var suser = EduWebHelper.BuildStudentSession(entity);
                EduWebHelper.SetStudentSession(suser);

                return Json(new { success = true, message = "", url = "/" });
            }
            return Json(result);
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            EduWebHelper.ClearStudentSession();
            return Redirect(FormsAuthentication.LoginUrl);
        }

        public ActionResult Index()
        {
            var session = EduWebHelper.GetStudentSession();
            var list = EduService.StudentCoursewarePeriod.GetList(session.StudentId, session.Student.WorkCategoryId);
            var totalPeriod = CurrentEduCategory.EduTotalPeriod;// AuthService.Param.Get("EduTotalPeriod", "72000").ToInt();
            ViewBag.totalPeriod = totalPeriod;
            ViewBag.dayPeriod = EduService.PeriodDetails.GetStudentDayPeriod(session.StudentId, DateTime.Now.Date);
            ViewBag.learnPeriod = session.Student.TotalPeriod;
            ViewBag.lastPeriodDetails = EduService.PeriodDetails.GetStudentLast(session.StudentId);
            ViewBag.progress = Convert.ToInt32(Convert.ToDecimal(session.Student.TotalPeriod) / totalPeriod * 100);
            if (ViewBag.progress >= 100)
            {
                ViewBag.progress = 100;
            }
            ViewBag.dicList = AuthService.DicDetail.GetListByDicCode("BusinessType");
            return View(list);
        }

        public ActionResult Learning(string coursewareId)
        {
            CurrentStudent.Learning = null;
            ViewBag.WorkCategoryId = CurrentStudent.Student.WorkCategoryId;
            ViewBag.Courseware = EduService.Courseware.Get(coursewareId);
            var detailsList = EduService.CoursewareDetails.GetList(coursewareId);
            return View(detailsList);
        }

        [AllowAnonymous]
        public ActionResult Integration(string idNum)
        {
            var studentService = EduService.Student;
            var model = studentService.GetByIDCard(idNum);
            if (model == null || model.Status != 2) //无效用户
            {
                FormsAuthentication.SignOut();
                EduWebHelper.ClearStudentSession();
                return Redirect(FormsAuthentication.LoginUrl);
            }

            FormsAuthentication.SetAuthCookie(model.IDCardNo, false);
            studentService.UpdateExamStatus(model);

            var audit = EduWebHelper.BuildStudentAuditEntity(model);
            EduService.StudentAudits.Create(audit);

            var suser = EduWebHelper.BuildStudentSession(model);
            suser.Device = "wechart";
            EduWebHelper.SetStudentSession(suser);

            return Redirect("/");
        }
    }
}